import {defHttp} from '/@/utils/http/axios';

enum Api {
  // 根据专利ID获取关联图谱 - 对应后端 GraphController
  getGraphList = '/analysis/graph/getGraphList',
  // 导出图谱数据
  exportGraph = '/analysis/graph/exportGraph',
}

/**
 * 根据专利ID获取关联图谱
 * @param patent_id 核心专利ID
 * @param type 关联类型: institution(申请机构), inventor(发明人), ipc(IPC分类), technology(技术领域)
 * @param maxNodes 最大节点数，默认20
 */
export const getGraphByPatentId = (patent_id: string, type: string = 'institution', maxNodes: number = 20) => {
  return defHttp.get({
    url: Api.getGraphList,
    params: {
      patent_id,
      type,
      maxNodes
    }
  });
};

/**
 * 导出图谱数据
 * @param params
 */
export const exportGraphData = (params) => {
  return defHttp.get({
    url: Api.exportGraph,
    params,
    responseType: 'blob'
  });
};

/**
 * 导出URL
 */
export const getExportUrl = Api.exportGraph;

/**
 * 保存图谱访问记录（自动从后端获取当前登录用户）
 * @param patentId 专利ID
 * @param patentName 专利名称
 * @param relationType 关联类型
 */
export const saveGraphHistory = (patentId: string, patentName: string, relationType: string) => {
  return defHttp.post(
    {
      url: '/analysis/graph/history/save',
      data: {  // 使用 data 发送请求体
        patentId,
        patentName,
        relationType
      }
    },
    { isTransformResponse: false }
  );
};

/**
 * 获取当前用户最后访问的图谱记录（自动从后端获取当前登录用户）
 */
export const getLastVisit = () => {
  return defHttp.get({
    url: '/analysis/graph/history/getLastVisit'
  });
};

